<?php

namespace addons\cloud_market\controller\admin;

use addons\cloud_market\model\CloudMarketAddon;
use addons\cloud_market\model\CloudMarketAddonVersions;
use addons\cloud_market\model\CloudMarketDeveloper;
use addons\cloud_market\model\CloudMarketDocs;
use addons\cloud_market\model\CloudMarketOrder;
use addons\cloud_market\model\CloudMarketQuestion;
use addons\cloud_market\model\CloudMarketSite;
use addons\cloud_market\model\CloudMarketSiteAddon;
use addons\cloud_market\model\CloudMarketSiteAddonToken;
use addons\cloud_market\model\CloudMarketSms;
use addons\cloud_market\model\CloudMarketUser;
use hiduoke\controller\AddonsController;

class Dashboard extends AddonsController
{
    protected $notLogin = ['*'];

    protected function initialize()
    {
        parent::initialize();
    }

    public function getDashboardData()
    {
        // 获取数据
        // 用户数量、模块数量、标签数量、文档数量
        // 今日：用户数量、模块数量、标签数量、文档数量、问题数量
        $allData = array(
            'user_count'             => (new CloudMarketUser())->count(),
            'addon_count'            => (new CloudMarketAddon())->count(),
            'order_count'            => (new CloudMarketOrder())->count(),
            'doc_count'              => (new CloudMarketDocs())->count(),
            'question_count'         => (new CloudMarketQuestion())->count(),
            'site_count'             => (new CloudMarketSite())->count(),
            'site_addon_count'       => (new CloudMarketSiteAddon())->count(),
            'site_addon_token_count' => (new CloudMarketSiteAddonToken())->count(),
            'sms_count'              => (new CloudMarketSms())->count(),
            'developer_1_count'      => (new CloudMarketDeveloper())->where('status', 1)->count(),
            'developer_0_count'      => (new CloudMarketDeveloper())->where('status', 0)->count(),
        );

        $todayData = array(
            'user_count'             => (new CloudMarketUser())->whereTime('create_time', 'today')->count(),
            'addon_count'            => (new CloudMarketAddon())->whereTime('create_time', 'today')->count(),
            'order_count'            => (new CloudMarketOrder())->whereTime('create_time', 'today')->count(),
            'doc_count'              => (new CloudMarketDocs())->whereTime('create_time', 'today')->count(),
            'question_count'         => (new CloudMarketQuestion())->whereTime('create_time', 'today')->count(),
            'site_count'             => (new CloudMarketSite())->whereTime('create_time', 'today')->count(),
            'site_addon_count'       => (new CloudMarketSiteAddon())->whereTime('create_time', 'today')->count(),
            'site_addon_token_count' => (new CloudMarketSiteAddonToken())->whereTime('create_time', 'today')->count(),
            'sms_count'              => (new CloudMarketSms())->whereTime('create_time', 'today')->count(),
            'addon_versions_count'   => (new CloudMarketAddonVersions())->whereTime('create_time', 'today')->count(),
        );


// 构建最近7个月的日期数组
        $data_x = [];
        for ($i = 6; $i >= 0; $i--) {
            $data_x[] = date('Ym', strtotime("-{$i} months"));
        }

// 初始化series数据
        $series = [
            'user'  => [],
            'order' => [],
            'site'  => [],
            'addon' => []
        ];

// 获取最近7个月的统计数据
        foreach ($data_x as $month) {
            // 修改: 使用正确的 whereBetweenTime 参数，传入开始和结束时间
            $startTime = date('Y-m-01 00:00:00', strtotime($month . '01'));
            $endTime = date('Y-m-t 23:59:59', strtotime($month . '01'));
            $series['user'][] = CloudMarketUser::whereBetweenTime('create_time', $startTime, $endTime)->count();
            $series['order'][] = CloudMarketOrder::whereBetweenTime('create_time', $startTime, $endTime)->count();
            $series['site'][] = CloudMarketSite::whereBetweenTime('create_time', $startTime, $endTime)->count();
            $series['addon'][] = CloudMarketAddon::whereBetweenTime('create_time', $startTime, $endTime)->count();
        }
        $echarts = [
            'data_x' => $data_x,
            'series' => $series
        ];


        return $this->success("ok", [
            'allData'   => $allData,
            'todayData' => $todayData,
            'echarts'   => $echarts
        ]);
    }

}
